﻿namespace KeRui.Upload
{
    using System;
    using System.IO;

    internal class MacFile
    {
        private MacBinary mMacBinary;
        private bool mOverwrite;
        private bool mUseStreams;

        public MacFile(MacBinary inMacBinary)
        {
            this.mMacBinary = inMacBinary;
            this.mUseStreams = true;
            this.mOverwrite = false;
        }

        public void Save(string inPath)
        {
            this.SaveData(inPath);
            try
            {
                if (this.mUseStreams)
                {
                    this.SaveStreams(inPath);
                }
            }
            catch (Exception exception)
            {
                File.Delete(inPath);
                throw exception;
            }
        }

        protected void SaveData(string inPath)
        {
            string str = inPath;
            FileUtils.WriteDataToFile(str, this.mOverwrite, this.mMacBinary.DataStream);
        }

        protected void SaveStreams(string inPath)
        {
            string str = inPath + ":AFP_Resource";
            string str2 = inPath + ":AFP_AfpInfo";
            Stream rezStream = this.mMacBinary.RezStream;
            if (rezStream.Length > 0L)
            {
                FileUtils.WriteDataToFile(str, this.mOverwrite, rezStream);
            }
            AfpInfo info = new AfpInfo();
            try
            {
                info.SetData(FileUtils.ReadDataFromFile(str2));
            }
            catch
            {
            }
            info.SetData(this.mMacBinary.MacType, this.mMacBinary.MacCreator, this.mMacBinary.Attr1, this.mMacBinary.Attr2);
            FileUtils.WriteDataToFile(str2, this.mOverwrite, info.data, 0, info.size);
        }

        public void SetOverwrite(bool inOverwrite)
        {
            this.mOverwrite = inOverwrite;
        }

        public void SetStreams(bool inUseStreams)
        {
            this.mUseStreams = inUseStreams;
        }

        public class AfpInfo
        {
            public byte[] data;
            public int size;

            public AfpInfo()
            {
                byte num;
                byte num2;
                byte num3;
                byte num4;
                byte num5;
                byte num6;
                this.size = 60;
                this.data = new byte[this.size];
                this.data[0] = 0x41;
                this.data[1] = 70;
                this.data[2] = 80;
                this.data[3] = 0;
                this.data[7] = (byte) (num = 0);
                this.data[6] = num2 = num;
                this.data[4] = this.data[5] = num2;
                this.data[6] = 1;
                this.data[11] = (byte) (num3 = 0);
                this.data[10] = num4 = num3;
                this.data[8] = this.data[9] = num4;
                this.data[15] = (byte) (num5 = 0);
                this.data[14] = num6 = num5;
                this.data[12] = this.data[13] = num6;
                this.data[15] = 0x80;
            }

            public void SetData(byte[] inData)
            {
                int length = (inData.Length > this.size) ? this.size : inData.Length;
                Array.Copy(inData, 0, this.data, 0, length);
            }

            public void SetData(uint inType, uint inCreator, byte inAttr1, byte inAttr2)
            {
                byte[] buffer = SuperStream.OSTypeToBytes(inType);
                for (int i = 0; i < 4; i++)
                {
                    this.data[0x10 + i] = buffer[i];
                }
                buffer = SuperStream.OSTypeToBytes(inCreator);
                for (int j = 0; j < 4; j++)
                {
                    this.data[20 + j] = buffer[j];
                }
                this.data[0x18] = (byte) (inAttr1 & 0xfc);
                this.data[0x19] = (byte) (inAttr2 & 0xfc);
            }
        }
    }
}

